package common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;

public class WebServiceUtil {

	public static enum HTTP_METHOD_TYPES {
		GET, POST
	}

	public static HttpURLConnection getHttpURLConnection(String url) {
		
//		System.out.println("WebServiceUtil: getHttpURLConnection(url: String): url value: " + url);
		
		URL myURL;
		HttpURLConnection conn = null;
		try {
			myURL = new URL(url);
			conn = (HttpURLConnection) myURL.openConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}
//		conn.setUseCaches(false);
//		System.out.println("Have set up a new connection...");
		
		return conn;
	}
	
	public static String getResponseAsString(HttpURLConnection conn) {
		
		InputStream stream = getUrlInputStream(conn);
		InputStreamReader is = null;
		StringBuffer sb = new StringBuffer();

		if (stream != null) {// If read time out, stream may be null
			is = new InputStreamReader(stream);
			BufferedReader in = new BufferedReader(is);
			String line = null;
			try {
				while ((line = in.readLine()) != null) {
					sb.append(line + "\n");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		if (sb.toString().length() == 0)
			return null;

		return sb.toString();
	}

	// write some content to the connection
	public static void writeStrToConnection(String str, HttpURLConnection conn) {
		
		OutputStream out = getUrlOutputStream(conn);
		try {
			out.write(str.getBytes());
			out.flush();
		} catch (IOException e) {

		} finally {
			try {
//				System.out.println("WebServiceUtil "+out==null);
				out.close();
			} catch (IOException e) {

			}
		}
	}

	private static OutputStream getUrlOutputStream(HttpURLConnection conn) {
		OutputStream os = null;
		try {
//			System.out.println("WebServiceUtil: connection to be connected");
			conn.connect();
//			System.out.println("WebServiceUtil: connection connected sucess!");
			os = conn.getOutputStream();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return os;
	}
	
	private static InputStream getUrlInputStream(HttpURLConnection conn) {
		InputStream is = null;
		try {
			conn.connect();
			is = conn.getInputStream();
		} catch (MalformedURLException e) {
			System.err
					.println("The url is illegal or the protocol is not supported!");
		} catch (IOException e) {
			System.err.println("Some IO exceptions occur!");
		}

		return is;
	}

	// set the request header properties
	public static void setRequestHeaderProperties(HttpURLConnection conn,
			Hashtable<String, String> properties) {
		Enumeration<String> keys = properties.keys();
		String key = null;
		while (keys.hasMoreElements()) {
			key = keys.nextElement();
			conn.setRequestProperty(key, properties.get(key));
		}
	}

	// set if allow write content to the connection
	public static void setAllowOutput(HttpURLConnection conn,
			boolean allowOutput) {
		conn.setDoOutput(allowOutput);
	}

	// set if allow read content from the connection
	public static void setAllowInput(HttpURLConnection conn, boolean allowInput) {
		conn.setDoInput(allowInput);
	}

	// set the HTTP Request Method Type
	public static void setHttpRequestMethodType(HttpURLConnection conn,
			HTTP_METHOD_TYPES methodType) {
		try {
			conn.setRequestMethod(methodType.toString());
		} catch (ProtocolException e) {
			System.out.println("HTTP Request Type Illegal! ");
			e.printStackTrace();
		}
	}
	
	public static void safeClose(HttpURLConnection conn) {
		conn.disconnect();
		conn = null;
//		System.out.println("Tear up the connection...");
	}
	
}
